package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
       List<ShoppingCart> list= shoppingCartMapper.list(shoppingCart);
       if (list !=null &&list.size()>0){
           for (ShoppingCart cart : list) {
               cart.setNumber(cart.getNumber()+1);
               shoppingCartMapper.update(cart);
           }

       }else {

           ShoppingCart shoppingCart1 = addFengzhuang(shoppingCartDTO, shoppingCart);
           shoppingCartMapper.add(shoppingCart1);

       }




    }

    @Override
    public List<ShoppingCart> list() {
        Long currentId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(currentId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        return list;
    }

    @Override
    public void subSoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        ShoppingCart shoppingCart1 = list.get(0);
        Integer number = shoppingCart1.getNumber();
        if (number>1){
            shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
            shoppingCartMapper.update(shoppingCart1);
        }else {
            shoppingCartMapper.deleteShoppingCat(shoppingCart1);
        }

    }

    @Override
    public void deleteAll() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.deleteShoppingCat(shoppingCart);
    }

    private ShoppingCart addFengzhuang(ShoppingCartDTO shoppingCartDTO, ShoppingCart shoppingCart) {
        Long dishId = shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();

        if (shoppingCartDTO.getDishId() != null) {
            DishVO dishVO = dishMapper.dishById(dishId);
            shoppingCart.setImage(dishVO.getImage());
            shoppingCart.setName(dishVO.getName());
            BigDecimal price = dishVO.getPrice();
            shoppingCart.setAmount(dishVO.getPrice());
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            return shoppingCart;
        } else {
            SetmealVO byid = setmealMapper.byid(setmealId);
            shoppingCart.setImage(byid.getImage());
            shoppingCart.setName(byid.getName());
            shoppingCart.setNumber(1);
            shoppingCart.setAmount(byid.getPrice());
            shoppingCart.setCreateTime(LocalDateTime.now());
            return shoppingCart;
        }

    }
}
